﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebBanThuoc.Server;
using WebBanThuoc.DAO;
using WebBanThuoc.DTO;
using WebBanThuoc.Function;

namespace WebBanThuoc.admin
{
    public partial class QLDonHang : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!FuncAdmin.IsLogin(Session))
            {
                Session.Add("URLCurrentActive_SendTo_Login", Request.RawUrl);
                Response.Redirect("/admin/DangNhap.aspx");
            }



            if (!IsPostBack)
            {

            }
            else
            {
                if (checkDefaultSubmit())
                {
                    string MaSanPham = Request.Params["MaDonHang"];
                    if (MaSanPham != null && MaSanPham.Trim() != "")
                    {
                        IServerWeb sw = new ServerWeb();
                        DonHang_DAO daoDH = new DonHang_DAO(sw.Connection);
                        DonHang dh = daoDH.getByKey("DH" + MaSanPham);                        
                        if (dh != null)
                        {
                            ChiTietDonHang_DAO daoCT = new ChiTietDonHang_DAO(sw.Connection);
                            List<ChiTietDonHang> dataCT = daoCT.getListByKey(dh.MaDonHang);
                            List<SanPham> dataSP = new List<SanPham>();
                            for (int i = 0; i < dataCT.Count; i++)
                            {
                                SanPham_DAO daoSP = new SanPham_DAO(sw.Connection);
                                SanPham sp = daoSP.getByKey(dataCT[i].MaSanPham);
                                if (sp != null)
                                {
                                    dataSP.Add(sp);
                                }
                                else
                                {
                                    sp = new SanPham() { TenSanPham = "" };
                                    dataSP.Add(sp);
                                }
                            }
                            
                            Session.Add("page_QLDonHang_dataDH", dh);
                            Session.Add("page_QLDonHang_dataCT", dataCT);
                            Session.Add("page_QLDonHang_dataSP", dataSP);
                            

                            //CachDung.Text = sp.CachDung;
                            ////ThanhPhan.Text = sp.ThanhPhan;
                            //CongDung.Text = sp.CongDung;
                        }
                        else
                        {
                            //CachDung.Text = "";
                            //ThanhPhan.Text = "";
                            //CongDung.Text = "";
                        }

                        sw.closeConnection();
                    }
                }
                else
                {
                    if (Request.Params["btn_Them"] != null)
                    {
                        add();
                    }
                    if (Request.Params["btn_Sua"] != null)
                    {
                        update();
                    }
                    if (Request.Params["btn_Xoa"] != null)
                    {
                        delete();
                    }
                }
            }

            this.LoadComplete += new EventHandler(QLDonHang_LoadComplete);
        }

        void QLDonHang_LoadComplete(object sender, EventArgs e)
        {
            getDataGeneral();
        }

        private bool checkDefaultSubmit()
        {
            if (Request.Params["btn_Xoa"] != null || Request.Params["btn_Sua"] != null || Request.Params["btn_Them"] != null)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// Lấy data mặc định cho page
        /// </summary>
        private void getDataGeneral()
        {
            IServerWeb sw = new ServerWeb();
            TinhTrangDonHang_DAO daoTT = new TinhTrangDonHang_DAO(sw.Connection);
            DonHang_DAO daoDH = new DonHang_DAO(sw.Connection);
            //SanPham_DAO daoSP = new SanPham_DAO(sw.Connection);
            List<TinhTrangDonHang> ttdh = daoTT.getListByKey();
            //List<LoaiSanPham> lsp = daoLSP.getListByKey();
            string msp = daoDH.createMaDonHang();
            Session.Add("page_QLDonHang_dataListTT", ttdh);
            //Session.Add("page_QLSanPham_dataListLSP", lsp);
            Session.Add("page_QLDonHang_MaDonHang_Moi", msp);
            sw.closeConnection();
        }

        private void add()
        {
            string MDH = Request.Params["MaDonHang"];
            if (MDH != null || MDH.ToString().Trim() == "")
            {
                DonHang dh = new DonHang();
                IServerWeb sw = new ServerWeb();
                DonHang_DAO daoDH = new DonHang_DAO(sw.Connection);
                dh.IDKhachHang = Convert.ToInt32(Request.Params["IDKhachHang"]);
                dh.MaTinhTrang = Request.Params["MaTinhTrang"].ToString();
                dh.NgayDat = Request.Params["NgayDat"].ToString() == "" ? DateTime.Now : Convert.ToDateTime(Request.Params["NgayDat"]);
                dh.TongTien = Function.Func.convertDoubleToString(Request.Params["TongTien"].ToString());
                dh.TongKhuyenMai = 0;
                dh.TenNguoiNhan = Request.Params["TenNguoiNhan"].ToString();
                dh.DiaChiNhan = Request.Params["DiaChiNhan"].ToString();
                dh.SoDienThoai = Request.Params["SoDienThoai"].ToString();
                dh.Email = Request.Params["Email"].ToString();
                dh.NgayCapNhat = DateTime.Now;
                dh.NgayGiao = Request.Params["NgayGiao"].ToString() == "" ? DateTime.MinValue : Convert.ToDateTime(Request.Params["NgayGiao"]);
                dh.GhiChu = Request.Params["GhiChu"].ToString();
                //sp.MaSanPham = daoSP.createMaSanPham();
                int tempRS = daoDH.add(dh);
                if (tempRS > 0)
                {
                    //DonHang dhNew = daoDH.getByKey(dh.MaDonHang);
                    Session.Add("page_QLDonHang_msgSucessFunc", "Đã thêm thành công Đơn hàng mới " + dh.MaDonHang);                    
                }
                sw.closeConnection();
            }
        }

        private void delete()
        {
            string MDH = Request.Params["MaDonHang"];
            if (MDH != null || MDH.ToString().Trim() != "")
            {

                IServerWeb sw = new ServerWeb();
                DonHang_DAO daoDH = new DonHang_DAO(sw.Connection);

                //sp.MaSanPham = daoSP.createMaSanPham();
                int tempRS = daoDH.deleteByKey("DH" + MDH);
                if (tempRS > 0)
                {
                    Session.Add("page_QLDonHang_msgSucessFunc", "Đã Xóa thành công Đơn hàng DH" + MDH);                    
                }
                sw.closeConnection();
            }
        }

        private void update()
        {
            string MDH = Request.Params["MaDonHang"];
            if (MDH != null || MDH.ToString().Trim() != "")
            {


                IServerWeb sw = new ServerWeb();
                DonHang_DAO daoDH = new DonHang_DAO(sw.Connection);
                //SanPham sp = daoSP.getByKey("SP" + Request.Params["MaSanPham"].ToString().Trim());
                DonHang dh = daoDH.getByKey("DH" + MDH.Trim());

                dh.IDKhachHang = Convert.ToInt32(Request.Params["IDKhachHang"]);
                dh.MaTinhTrang = Request.Params["MaTinhTrang"].ToString();
                dh.NgayDat = Request.Params["NgayDat"].ToString() == "" ? DateTime.Now : Convert.ToDateTime(Request.Params["NgayDat"]);
                dh.TongTien = Function.Func.convertDoubleToString(Request.Params["TongTien"].ToString());
                dh.TongKhuyenMai = 0;
                dh.TenNguoiNhan = Request.Params["TenNguoiNhan"].ToString();
                dh.DiaChiNhan = Request.Params["DiaChiNhan"].ToString();
                dh.SoDienThoai = Request.Params["SoDienThoai"].ToString();
                dh.Email = Request.Params["Email"].ToString();
                dh.NgayCapNhat = DateTime.Now;
                dh.NgayGiao = Request.Params["NgayGiao"].ToString() == "" ? DateTime.MinValue : Convert.ToDateTime(Request.Params["NgayGiao"]);
                //sp.MaSanPham = daoSP.createMaSanPham();
                dh.GhiChu = Request.Params["GhiChu"].ToString();
                int tempRS = daoDH.updateByKey(dh, dh.MaDonHang);
                if (tempRS > 0)
                {
                    DonHang dhNew = daoDH.getByKey(dh.MaDonHang);
                    Session.Add("page_QLDonHang_msgSucessFunc", "Đã Cập nhật thành công Đơn hàng " + dhNew.MaDonHang);
                    Session.Add("page_QLDonHang_dataDH", dhNew);                    
                }
                sw.closeConnection();
            }
        }
    }
}